--
-- @Author:      feilong
-- @DateTime:    2019-12-19 15:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local constant = require("constant")
local reason = require("reason")
local cjson = require("cjson")

local table_insert = table.insert
local table_remove = table.remove
local math_random = math.random



local MessageHandler = class("MessageHandler")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function MessageHandler:ctor(message_dispatch)
    self.message_dispatch = message_dispatch    

    self:register()
end

--注册本服务里的消息
function MessageHandler:register()
    --启动
    self.message_dispatch:registerSelf('start', handler(self,self.start))
    self.message_dispatch:registerSelf('stop', handler(self,self.stop))

    self.message_dispatch:registerSelf('get_reapack_award', handler(self,self.getRedpackAward))
    self.message_dispatch:registerSelf('get_redpack_state', handler(self,self.getRedpackState))
    self.message_dispatch:registerSelf('get_state', handler(self,self.getActState))    

end


function MessageHandler:createUser(uid)
    local user = self.user_manager:addUser(uid) 
    local act_data = self.config:getActivity() --活动配置
    local ac_content = act_data.ac_content or {}       
    local counts = ac_content.pack_num or {1, 10}
    local max_count = math_random(counts[1], counts[2])
    user:setMaxRedpackNum(max_count)    
    return user   
end

------------------------------------------------------------------
--cmd
------------------------------------------------------------------
function MessageHandler:start(act_id)
    print("______redpack_start_______")
    self.act_id = act_id
    self.config = global.config 
    self.user_manager = global.user_manager
    self.desk = global.desk
    self.config:start(act_id)
    self.desk:start()

    -- self.act_data = self.config:getActivity() --活动配置

end

function MessageHandler:stop()
    self.desk:stopTimer()
    skynet.sleep(10)    
    dbglog("_______活动关闭________",self.act_id)
    skynet.exit()
end

--点击红包
function MessageHandler:getRedpackAward(msg)
    if self.desk:getState() ~= "grant" then 
        print("____还没红包触发____")
        return nil
    end
    local uid = msg.uid        
    local user = self.user_manager:getUser(uid)
    if not user then         
        user = self:createUser(uid)
        if not user then 
            errlog("_____用户不存在__",uid)
            return 
        end     
    end 
    if user:getMaxRedpackNum() <= user:getRedpackNum() then 
        --超过可领取个数
        user:setRedpackCoins(0)
        return {
            result = -1,
            description = "可抢红包次数已用完",
        }
    end     
    local coins = self.desk:checkWhiteName(user)
    if not coins then 
        coins = self.desk:getPackCoin(user)
    end
    if coins > 0 then         
        self.desk:addChip(user, coins, reason.COINS_STORM)
    end
    self.desk:addPartakeNum(uid) --记录参与的人
    user:setRedpackNum(user:getRedpackNum() + 1) --抢的次数
    local res = {
        redpack = {
            gain_count = user:getRedpackNum(),
            max_count = user:getMaxRedpackNum(),
            coins = user:getRedpackCoins(),
            all_coins = user:getRedpackAllCoins(),
            countdown = self.desk:getLeftTime(),
            is_open = self.desk:getStateNum() - 1,  
        }
    }
    dbglog("_____getRedpackAward___res____", res)

    self.desk:opPartakeNum(uid, constant.DODULE_ID.GOLD_STORM)
    return res
end

--取红包状态
function MessageHandler:getRedpackState(msg)
    local uid = msg.uid    
    local user = self.user_manager:getUser(uid)
    if self.desk:getState() == "grant" then 
        if not user then 
            user = self:createUser(uid)
            if not user then 
                errlog("_____用户不存在__",uid)
                return
            end
        end
    end

    local today_pay_money = msg.today_pay_money
    local act_data = self.config:getActivity() --活动配置
    local ac_content = act_data.ac_content or {}    
    local need_recharge = ac_content.need_recharge or 0

    local gain_count = 0
    local max_count = 0
    local all_coins = 0
    if user then 
        gain_count = user:getRedpackNum()
        max_count = user:getMaxRedpackNum()
        all_coins = user:getRedpackAllCoins()
    end
    local res = {
        redpack = {
            gain_count = gain_count,
            max_count = max_count,
            coins = 0,
            all_coins = all_coins,
            countdown = self.desk:getLeftTime(),
            is_open = self.desk:getStateNum() - 1,  
            state_end_time = self.desk:getStateEndTime(),
            need_recharge = need_recharge, --进入游戏今日需要充值的金额
            today_recharge = today_pay_money, --今天的充值数
        }
    }    
    dbglog("_____getRedpackState___res____", res)
    return res    
end

--活动列表中的状态
function MessageHandler:getActState()
    local res = {
        redpack = {
            gain_count = 0,
            max_count = 0,
            coins = 0,
            all_coins = 0,
            countdown = self.desk:getLeftTime(),
            is_open = self.desk:getStateNum() - 1,  
            state_end_time = self.desk:getStateEndTime(),
        }
    }    
    dbglog("_____getActState___res____", res)
    return res    
end




return MessageHandler